/**
 * 
 */
package com.narunas;

import java.util.ArrayList;

import com.narunas.containers.ContainerBuilder;
import com.narunas.shapes.ChildShapeOval;
import com.narunas.shapes.ContainerView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Interpolator;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;

/**
 * @author skraiduolis@gmail.com
 *
 */
public class ScalingTest extends Activity{

	//ArrayList children;
	ViewGroup container;
	Animation pAnimation;
	LayoutAnimationController controller;
	Animation cAnimation;
	ViewGroup cA;
	View child;
	TextView tt;
	
	Float scaleVector;
	Float cScaleVector;
	
	public Menu subMenu;
	
	
	private static final int scalingMenuResources[] = { R.menu.submenu_scaling};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        
	
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		super.onCreate(savedInstanceState);
		
		container = ContainerBuilder.createScalingContainer(this);
		setContentView(container);
		
		
		// definig the elements
		cA = (ViewGroup)container.getChildAt(1);
		child = cA.getChildAt(0);
		tt = (TextView) child.findViewById(100);
		//Log.d("EPX_Scaling", "the TextView :" + tt.toString());
		
		//scaleVector = 1.0f;
		//cScaleVector = 1.0f;
		
		
		
	};
	
	
	protected final void handleAnimation() {

		AccelerateDecelerateInterpolator ai = new AccelerateDecelerateInterpolator();
		
		/**
		 * 
		 *  PARENT LAYOUT animation
		 * 
		 * 
		 */
		
		pAnimation = new ScaleAnimation(

				 Animation.RELATIVE_TO_SELF, scaleVector,
				 Animation.RELATIVE_TO_SELF, scaleVector,
				 Animation.RELATIVE_TO_SELF, 0.5f, 
				 Animation.RELATIVE_TO_SELF, 0.5f);
		
		
		
		pAnimation.setInterpolator(ai);
		pAnimation.setStartOffset(200);
		pAnimation.setDuration(500);
		pAnimation.setFillBefore(true);
		pAnimation.setFillAfter(true);
		
		
		//controller = new LayoutAnimationController(pAnimation);
		cA.setAnimation(pAnimation);
		//cA.startAnimation(pAnimation);
		 
		 
		 
		/**
		 * 
		 *  CHILD animation
		 * 
		 */
		
		cAnimation = new ScaleAnimation(
				
				Animation.RELATIVE_TO_SELF, cScaleVector,
				Animation.RELATIVE_TO_SELF, cScaleVector,
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f
			);
		
		cAnimation.setInterpolator(ai);
		cAnimation.setStartOffset(200);
		cAnimation.setDuration(500);
		cAnimation.setFillAfter(true);
		 
		
		cAnimation.setAnimationListener(onAnimationListener);
		child.setAnimation(cAnimation);
		 //child.startAnimation(cAnimation);
		 
		
	}
	
	
	
	// unused
	protected void processChildAnimations() {
		/*
		 AccelerateDecelerateInterpolator ai = new AccelerateDecelerateInterpolator();
		Animation animation = new ScaleAnimation(
				
				Animation.RELATIVE_TO_SELF, cScaleVector,
				Animation.RELATIVE_TO_SELF, cScaleVector,
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f
			);
		
		animation.setInterpolator(ai);
		 animation.setStartOffset(200);
		 animation.setDuration(500);
		 animation.setFillAfter(true);
		 
		*/
		//TextView tt = child.getFocusables(1)[0];
		//child.forceLayout();
		
		//Log.d("EPX_Scaling", "the TextView parent :" + child.getLeft()+ " : " + child.getTop()+" : " + child.getRight() + " : " + child.getBottom());
		//tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));		
		tt.invalidate();
		
		//tt.postInvalidate((int)(child.getLeft()/ scaleVector), (int)(child.getTop() / scaleVector), (int)(child.getRight() / scaleVector), (int)(child.getBottom() / scaleVector));
		//cA.requestLayout();
		
		
		
		//child.setAnimation(animation);
		
	
	}
	
	AnimationListener onAnimationListener = new AnimationListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			processChildAnimations();
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
		
	};
	
	// Bottom MENU stuff
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      
        subMenu= menu;
        
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(scalingMenuResources[0], menu);      
        return true;
        
    };
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            
        	case R.id.submenu_scale_up:
        		
        		
				scaleVector 	= 2.0f;		
				cScaleVector 	= 0.5f;
				handleAnimation();
				
				
            return true;
            
        	case R.id.submenu_scale_down:
        		
        		
				scaleVector 	= 0.5f;
				cScaleVector 	= 2.0f;
        		handleAnimation();
        		
        		
        	return true;
        	
        	case R.id.submenu_scale_nominal:
        		
				scaleVector = 1.0f;
				cScaleVector = 1.0f;
				
        		handleAnimation();
        		
        	return true;
        }
        
        return false;
    }
}
